/*
 * @(#)DrawConfigImpl.java   09/09/13
 *
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 *
 *
 *
 */



package fr.ema.arch3d.gea.config;

import fr.ema.arch3d.gea.port.MyCellViewFactory;
import fr.ema.arch3d.model.architecture.configuration.ComponentClass;
import fr.ema.arch3d.model.architecture.configuration.Configuration;
import fr.ema.arch3d.model.architecture.configuration.ConnectorClass;

import org.jgraph.JGraph;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.GraphLayoutCache;
import org.jgraph.graph.GraphModel;

import org.objectweb.fractal.api.NoSuchInterfaceException;

import java.util.List;

import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextPane;

/**
 * Class description
 *
 *
 * @version        1.0, 09/09/13
 * @author         Huaxi (Yulin) ZHANG
 */
public class DrawConfigImpl {

    /** Field description */
    public static JScrollPane cGraph;

    /** Field description */
    public static JTextPane cText;

    /** Field description */
    public static JScrollPane cTree;

    /** Field description */
    public Configuration config;

    /** Field description */
    public JGraph thisGraph;

    public JSplitPane drawConfig(Configuration config) {
        this.config = config;

        // TODO Auto-generated method stub
        GraphModel model = new DefaultGraphModel();

        /*
         * GraphLayoutCache view = new GraphLayoutCache(model,
         * new DefaultCellViewFactory());
         */
        GraphLayoutCache   view  = new GraphLayoutCache(model, (new MyCellViewFactory()));
        JGraph             graph = new JGraph(model, view);
        DefaultGraphCell[] cells = null;

        // tree

        /*
         * MyGraphTreeModel gtModel = new MyGraphTreeModel(graph.getModel());
         * JTree tree = new JTree(gtModel);
         * graph.getModel().addGraphModelListener(gtModel);
         * tree.setRootVisible(false);
         */

        // draw vertext
        DrawVertex dv = new DrawVertex();

        try {
            List cs = config.getCmpClasses();

            cells = new DefaultGraphCell[cs.size()];

            for (int i = 0; i < cells.length; i++) {
                cells[i] = dv.drawVertext((ComponentClass) cs.get(i), i);

                // GraphConstants.setAutoSize(cells[i].getAttributes(),true);
            }
        } catch (NoSuchInterfaceException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // draw edges
        DrawEdge de             = new DrawEdge();
        List     connector_list = config.getConnectorClasses();
        MyEdge[] edges          = null;

        edges = new MyEdge[connector_list.size()];

        for (int i = 0; i < edges.length; i++) {
            edges[i] = de.drawEdge(cells, (ConnectorClass) connector_list.get(i));
        }

        graph.getGraphLayoutCache().insert(cells);
        graph.getGraphLayoutCache().insert(edges);
        graph.setPortsVisible(true);
        cGraph = new JScrollPane(graph);

        MyTreeConfig tree = new MyTreeConfig(model);

        // graph.getModel().addGraphModelListener(gtModel);
        // tree.setRootVisible(false);
        cTree = new JScrollPane(tree.s);

        AdlText t = new AdlText();

        cText = new JTextPane();
        cText.setDocument(t.createTextConfig(config));
        cText.setEditable(false);

        JSplitPane pane1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, cTree, cGraph);
        JSplitPane pane  = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, pane1, cText);

        graph.addMouseListener(new MouseEventConfig(this));
        pane.setSize(1100, 300);
        pane1.setDividerLocation(180);
        pane.setDividerLocation(900);
        thisGraph = graph;

        return pane;
    }
}
